Introduction
A simple program to add two numbers in Android.
Procedure
- Start Eclipse IDE.
- Create a new project.
- Create a MainActivity.java file.
- Create an activity_main.xml file for layout design.
- Add a button and three TextView fields in the XML layout.
- Then look up the button and EditText fields by their ids in the MainActivity.java file.
- Then define an additional code in the onClick function.
The following is the code.
MainActivity.java
- package com.example.additionof2no;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class MainActivity extends Activity implements OnClickListener {
- EditText e1, e2, e3;
- Button b1;
- double d1, d2, d3;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- b1 = (Button) findViewById(R.id.b2);
- e1 = (EditText) findViewById(R.id.e1);
- e2 = (EditText) findViewById(R.id.e2);
- e3 = (EditText) findViewById(R.id.e3);
- b1.setOnClickListener(this);
- }
- public void onClick(View v) {
- d1 = Double.parseDouble(e1.getText().toString());
- d2 = Double.parseDouble(e2.getText().toString());
- d3 = d1 + d2;
- e3.setText(Double.toString(d3));
- }
- }
activity_main.xml
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
-
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/hydrangeas"
- android:orientation="vertical"
- >
-
- <LinearLayout android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="FN"/>
-
- <EditText android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/e1"
- android:hint="Enter First Number"/>
-
- </LinearLayout>
-
- <LinearLayout android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="SN"/>
-
- <EditText android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/e2"
- android:hint="Enter Second Number"/>
-
- </LinearLayout>
-
- <LinearLayout android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <TextView android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Result"/>
-
- <EditText android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/e3"
- android:hint="Result is here"/>
-
- </LinearLayout>
-
- <LinearLayout android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
-
- <Button android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/b2"
- android:text="Click here to add"/>
-
- </LinearLayout>
-
- </LinearLayout>
Output